package com.zmn.plat.admin.controller.channel.workorder.video;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.plat.common.exception.PlatException;
import com.zmn.plat.model.entity.channel.workorder.video.ChannelWorkOrderVideo;
import com.zmn.plat.model.entity.channel.workorder.video.ChannelWorkOrderVideoQuery;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;
import java.util.Optional;

/**
 * 类描述：渠道-工单视频
 *
 * @author yangshuangping
 * @since 2021/06/07 14:00
 */
@RestController
@RequestMapping("/channel/workorder/video/")
public class ChannelWorkOrderVideoModifyController extends ChannelWorkOrderVideoBaseController {

    /**
     * 新增或根据工单视频ID修改
     *
     * @param video 新增或修改实体
     * @return ResponseDTO 返回实体
     */
    @RequestMapping(value = "addEdit")
    @RequiresPermissions("/channel/workorder/video/addEdit.action")
    public ResponseDTO addEdit(@RequestBody ChannelWorkOrderVideo video) {

        try {

            video.setUpdater(getMcStaffRealName());
            video.setUpdateTime(DateUtil.getNow());
            video.setCategName(servCategoryService.findByKey(video.getServCategId()).getShowName());

            if (NumberUtil.isNullOrZero(video.getVideoId())) {
                checkChannel(video);
                Integer maxSortValue = Optional.ofNullable(channelVideoService.findMaxSort()).orElse(0);
                video.setSort(++maxSortValue);
                video.setCreater(getMcStaffRealName());
                video.setCreateTime(DateUtil.getNow());
                channelVideoService.insert(video);
            } else {
                channelVideoService.updateByKey(video);
            }
            return ResponseDTO.success(true, "操作成功！");
        } catch (DuplicateKeyException ex) {
            logger.error("#channel-insert-video-thereIsSameData#, channelId:{}", video.getChannelId(), ex);
            return ResponseDTO.fail("已存相同的工单视频，无需重复添加！");
        } catch (Exception e) {
            logger.error("#channel-insert-video-Fail#,Exception{},msg{}", e, e.getMessage());
            return ResponseDTO.fail(e.getMessage());
        }
    }

    /**
     * 修改排序
     *
     * @param id   工单视频ID
     * @param sort 排序号
     * @return ResponseDTO
     */
    @RequestMapping(value = "updateSort")
    @RequiresPermissions("/channel/workorder/video/list.action")
    public ResponseDTO updateSort(@RequestParam(value = "id", defaultValue = "0") Integer id
            , @RequestParam(value = "sort", defaultValue = "0") Integer sort) {

        if (NumberUtil.isNullOrZero(id)) {
            return ResponseDTO.fail("请选择要更改排序的数据！");
        }
        if (NumberUtil.isNullOrZero(sort)) {
            return ResponseDTO.fail("请设置排序值！");
        }
        ChannelWorkOrderVideo video = new ChannelWorkOrderVideo();
        copyVideoVo(video, id, getMcStaffRealName());
        video.setSort(sort);
        channelVideoService.updateByKey(video);
        return ResponseDTO.success(true, "操作成功！");
    }

    /**
     * 修改后台审核标志
     *
     * @param id        工单视频ID
     * @param auditFlag 后台审核：1不审核  2要审核
     */
    @RequestMapping(value = "updateAuditFlag")
    @RequiresPermissions("/channel/workorder/video/updateAuditFlag.action")
    public ResponseDTO updateAuditFlag(@RequestParam(value = "id", defaultValue = "0") Integer id
            , @RequestParam(value = "auditFlag", defaultValue = "1") Integer auditFlag) {

        if (NumberUtil.isNullOrZero(id)) {
            return ResponseDTO.fail("请选择要更改状态的数据！");
        }
        ChannelWorkOrderVideo video = new ChannelWorkOrderVideo();
        copyVideoVo(video, id, getMcStaffRealName());
        video.setAuditFlag(auditFlag);
        channelVideoService.updateByKey(video);
        return ResponseDTO.success(true, "操作成功！");
    }

    /**
     * 修改必传标志
     *
     * @param id           工单视频ID
     * @param requiredFlag 是否必传 1不必传  2必传
     */
    @RequestMapping(value = "updateRequiredFlag")
    @RequiresPermissions("/channel/workorder/video/updateRequiredFlag.action")
    public ResponseDTO updateRequiredFlag(@RequestParam(value = "id", defaultValue = "0") Integer id
            , @RequestParam(value = "requiredFlag", defaultValue = "1") Integer requiredFlag) {
        if (NumberUtil.isNullOrZero(id)) {
            return ResponseDTO.fail("请选择要更改状态的数据！");
        }
        ChannelWorkOrderVideo video = new ChannelWorkOrderVideo();
        copyVideoVo(video, id, getMcStaffRealName());
        video.setRequiredFlag(requiredFlag);
        channelVideoService.updateByKey(video);
        return ResponseDTO.success(true, "操作成功！");
    }

    /**
     * 根据工单视频ID删除信息
     *
     * @param videoId 工单视频ID
     * @return ResponseDTO 返回实体
     */

    @RequestMapping(value = "delete")
    @RequiresPermissions("/channel/workorder/video/delete.action")
    public ResponseDTO delete(@RequestParam(value = "videoId", defaultValue = "0") Integer videoId) {

        if (NumberUtil.isNullOrZero(videoId)) {
            return ResponseDTO.fail("请选择要删除的数据！");
        }
        ChannelWorkOrderVideo video = channelVideoService.findByKey(videoId);
        if (Objects.isNull(video)) {
            return ResponseDTO.fail("未获取到相关记录！");
        }
        channelVideoService.deleteByKey(videoId);
        return ResponseDTO.success(true, "操作成功！");

    }

    /**
     * 设置修改时间
     *
     * @param channelVideo 实体
     * @param id           ID
     * @param updater      修改人
     */
    private void copyVideoVo(ChannelWorkOrderVideo channelVideo, Integer id, String updater) {
        channelVideo.setVideoId(id);
        channelVideo.setUpdater(updater);
        channelVideo.setUpdateTime(DateUtil.getNow());
    }

    /**
     * 新增渠道工单视频校验
     *
     * @param video 新增实体
     */
    private void checkChannel(ChannelWorkOrderVideo video) {

        ChannelWorkOrderVideoQuery query = new ChannelWorkOrderVideoQuery();
        query.setChannelId(video.getChannelId());
        query.setServCategId(video.getServCategId());
        // 服务分类不能重复
        if (channelVideoService.countByQuery(query) > 0) {
            throw new PlatException("该服务分类已存在 ");
        }
    }
}
